<?php
namespace Home\Controller;
use Think\Controller;
class CarController extends CommonController {
    public function setCar(){
    	// var_dump($_POST);
        // var_dump($_POST['fid']);
        // $uid = $_SESSION['id'];
        $uid = $_SESSION['id'];
        //创建car表的模型
        $car = M('mvp_car');
        $carrinfo = $car->where('uid = '.$uid)->select();
        if(empty($carrinfo)){
            $this->error('你的购物车为空', U('Home/Forms/index'),3);
            return false;
        }
        $form = M('mvp_forms');
        $formfoods = M('mvp_formfoods');
        $carinfo = $car->select();
        // var_dump($carinfo);
        $_POST['ftime'] = $ftime = time();//生成订单的时间
        $_POST['formnumber'] = $formnumber = $ftime.$_SESSION['id'];//订单号
        $ask = $_POST['ask'];
        $_POST['uid'] = $uid = $_SESSION['id'];
        $fprice = $_POST['fprice'];//总价

        //创建form表的模型
        //var_dump($_POST);
        $form->create();
        $forminfo = $form->add();
        // var_dump($forminfo);
        if($forminfo){
            foreach ($carinfo as $key => $value) {
                $carinfo[$key]['sell'] = $carinfo[$key]['sprice'];
                $carinfo[$key]['number'] = $_POST['number'][$key];
                $carinfo[$key]['formfoodsnumber'] = $_POST['formnumber'];
            }
            // var_dump($carinfo);
            foreach ($carinfo as $k => $v) {
               array_shift($v);
               $formfoods->create($v);
               $formfoodsinfo[] = $formfoods->add();
            }
            // echo $formfoods->_sql();
             // var_dump($formfoodsinfo);
            //创建formfoods模型
        }else{
            $this->error('提交失败', U('Home/Car/index'),3);
        }
        //查form表里的订单时间
        // $formtimeinfo=$form->where('formnumber = '.$formnumber)->find();
        // var_dump($formtimeinfo);
        // $this->assign('formtimeinfo',$formtimeinfo);
        //查foods表的信息
        $foodsforminfo = $formfoods->where('mvp_formfoods.formfoodsnumber = '.$formnumber)->join('mvp_foods on mvp_formfoods.fid = mvp_foods.id')->select();
        // var_dump($_POST);
        $this->assign('foodsforminfo',$foodsforminfo);
        $this->assign('forminfo',$_POST);
        $del = $car->where('uid = '.$uid)->delete();
        // echo $car->_sql();
        // var_dump($del);
        $this->display();
    }
    public function index(){
    	
         // var_dump($_GET);
        if(is_numeric($_GET['cid'])){
            $isnumber = 'cid';
        }else{
            if($_GET['cid'] == 'true'){
                $isnumber = 'iscid';
            }else{
                $isnumber = 'keywords';
            }
        }

    	//类名id
    	$cid=$_GET['cid'];
    	//创建提交订单购物车表的模型
    	$tjcar = M('mvp_car');
        $tj = $tjcar->where('uid = '.$_SESSION['id'])->select();
        if(empty($tj)){
            $this->error('饿了么???,先去添加喜欢的美食去吧!!!', U('Home/Index/index'),3);
            return false;
        }
    	$foods = M('mvp_foods');
    	$uid = $_SESSION['id'];
    	// var_dump($uid);
    	//通过uid去查用户信息
    	$user = M('mvp_info');
    	$cinfo = $user->where('uid='.$uid)->find();
    	//根据car表里的去查foods表里的信息
    	$tjinfo=$tjcar->where("mvp_car.uid = ".$uid)->join("mvp_foods on mvp_foods.id = mvp_car.fid")->select();
    	// echo $tjcar->_sql();
        //总优惠
        $arr = array();
        foreach ($tjinfo as $key => $value) {
            $arr[$key] = $value['sprice']*$value['number'];

        }
        $carsell = array_sum($arr);
        // var_dump($cinfo);
        // var_dump($tjinfo);
        
    	$this->assign("tjinfo",$tjinfo);//分配提交订单信息	
    	$this->assign('cid',$cid);//分配类名id
    	$this->assign('cinfo',$cinfo);//分配用户信息
        $this->assign('carsell',$carsell);//分配优惠总价
        $this->assign('isnumber',$isnumber);
    	$this->display();
	}
    public function jrCar(){
    	// var_dump($_POST);
        if(empty($_POST['fid'])){
            $_POST['fid']=$_GET['fid'];
            $_POST['sprice'] = $_GET['sellprice'];        
        }
    	//var_dump($_SESSION);
    	//创建购物车表的模型
    	$car = M('mvp_car');
        $where['uid'] = array('eq',$_SESSION['id']);
        $where['fid'] = array('eq',$_POST['fid']);
        // var_dump($where);die;
    	$carin = $car->where($where)->find();
    	// echo $car->_sql();
        // var_dump($_POST);
    	// var_dump($carin);die;
    	if(!empty($carin)){
    		// echo 22222222;
    		// var_dump($carin);
    		$number = $carin['number'] + 1;
    		// var_dump($number);
    		$carin1 = $car->execute('update mvp_car set number = '.$number." where fid = ".$carin['fid']);
    		// echo ($car->_sql());
             // var_dump($carin1);
            if($carin1>0){
                echo 1;
            }else{
                echo 0;
            }
    		return false;
    	}
        $_POST['uid'] = $_SESSION['id'];
        $_POST['number'] = 1;
    	// $carinfo = $car->query("insert into mvp_car (uid,fid,number,sprice) value('{$uid}','{$fid}','{$number}','{$sprice}')");
    	$car->create();
        $carinfo = $car->add();
        // echo $car->_sql();
    	// var_dump($carinfo);

    	if($carinfo>0){
				echo 1;
			}else{
				echo 0;
			}
        }
    public function cajax(){
    	// var_dump($_POST);
    	//创建删除购物车表的模型
    	$scar = M('mvp_car');
        $where['uid'] = array('eq',$_SESSION['id']);
        $where['fid'] = array('eq',$_POST['fid']);
    	$scinfo = $scar->where($where)->delete();
    	// echo $scar->_sql();
    	if($scinfo>0){
    		echo 1;
    	}else{
    		echo 0;
    	}
    }
    
}
